package com.example.demo.account.entity;

import com.example.demo.information.entity.Student;
import lombok.Data;
import org.hibernate.annotations.GeneratorType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * 描述：用户表
 * @author littlecar
 * @date 2019/9/5 9:55
 */
@Entity
@Table(name = "user")
@Data
public class SysUser implements Serializable {

    /**
     * 主键id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    /**
     * 用户名
     */
    @Column(name = "username",length = 50,nullable = false)
    private String username;

    /**
     * 密码
     */
    @Column(name="password",length = 100,nullable = false)
    private String password;

    /**
     * 联系电话
     */
    @Column(length = 50,nullable = false)
    private String mobile;

    /**
     * 性别
     */
    @Column(length = 5,nullable = false)
    private String sex;

    /**
     * 邮箱
     */
    @Column(length = 100,nullable = false)
    private String email;
    /**
     * 姓名
     */
    @Column(length = 50,nullable = false)
    private String name;
    /**
     * 身份证号
     */
    @Column(length = 50,nullable = false)
    private String sfz;

    /**
     * 用户的角色集合
     */
    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name="user_id")
    private Set<SysUserRole> sysUserRoles = new HashSet<SysUserRole>();

    /**
     * 状态：1启用，0禁用
     */
    @Column(length = 2,nullable = false)
    private Integer status;

    public SysUser() {
    }

    public SysUser(String username, String password, String mobile, String sex, Set<SysUserRole> sysUserRoles,Integer status) {
        this.username = username;
        this.password = password;
        this.mobile = mobile;
        this.sex = sex;
        this.sysUserRoles = sysUserRoles;
        this.status = status;
    }

    public Set<SysUserRole> getSysUserRoles() {
        Set<SysUserRole> sysUserRoles = this.sysUserRoles;
        TreeSet<SysUserRole> sortedSet = new TreeSet<>(((o1, o2) -> o1.compareTo(o2)));
        sortedSet.addAll(sysUserRoles);
        return sortedSet;
    }
    public SysUser(Student student){
        this.name = student.getName();
        this.sfz = student.getSfz();
        this.sex = student.getSex();
        this.username = student.getSfz();
        this.mobile = "130000000000";
        this.email = "temp@email.com";
        this.status = 1;
    }

}
